package com.seed.core;

import cn.hutool.json.JSONUtil;
import com.seed.core.pojo.R;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class ResponseWrapper {

    private final HttpServletResponse response;

    public ResponseWrapper(HttpServletResponse response) {
        this.response = response;
    }

    public void sendJSON(Object data) {
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        try {
            PrintWriter out = response.getWriter();
            out.println(JSONUtil.toJsonStr(data));

            out.flush();
            out.close();
        } catch (IOException e) {
            throw new RuntimeException("服务器端异常", e);
        }
    }

    public void send404() {
        sendJSON(R.error("404"));
    }

    public HttpServletResponse getResponse() {
        return response;
    }
}
